<template>
  <div class="home-container">
    <!-- 导航 -->
    <!-- 按下ctrl+shift-z查看文档 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        icon="search"
        class="search-btn"
        round
        type="info"
        size="small"
        slot="title"
        @click="$router.push('/search')"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 标签栏 -->
    <van-tabs v-model="active" animated swipeable class="channel-tabs">
      <van-tab
        :title="item.name"
        v-for="(item, index) in channels"
        :key="index"
      >
        <article-list :channel="item"></article-list>
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="showEditChannel = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 编辑频道弹层 -->
    <van-popup
      v-model="showEditChannel"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-left"
    >
      <channel-edit :channels="channels" :active="active" @add="channels.push($event)" @update-active="handleUpdateActive"></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/channels'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
export default {
  name: 'HomePage',
  data () {
    return {
      active: 0,
      channels: [],
      showEditChannel: false // 控制弹层显示
    }
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  async created () {
    const res = await getUserChannels()
    if (this.$store.state.token) {
      // 如果登录,发请求
      this.channels = res.data.data.channels
    } else {
      // 没有登录,获取本地
      try {
        this.channels = JSON.parse(localStorage.toutiao_channels)
      } catch (error) {
        // 如果没有本地数据,给默认数据
        this.channels = res.data.data.channels
      }
    }
  },
  mounted () {},
  methods: {
    handleUpdateActive (index, isShow = false) {
      this.active = index
      this.showEditChannel = isShow
    }
  }
}
</script>

<style lang="less" scoped>
// 样式不起作用
// (1)级别不够
// (2)父组件没有影响子组件的样式
// 有scoped,生成样式,scoped会给组件里面所用标签添加data-v-组件id这样的书写,
//.van-nav-bar__title[data-v-5954443c]
// 没有scoped,生成样式
// van-nav-bar__title
// 使用深度选择符 /deep/
// [data-v-5954443c] .van-nav-bar__title
/deep/ .van-nav-bar__title {
  max-width: unset !important;
}
.search-btn {
  width: 555px;
  height: 64px;
  background-color: #5babfb;
  border: none;
  font-size: 28px;
  .van-icon {
    font-size: 32px;
  }
}
.home-container {
  padding-top: 172px;
}
/deep/ .channel-tabs {
  .van-tabs__wrap {
    position: fixed;
    top: 92px;
    z-index: 100;
    width: 100%;
  }
  .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
    height: 80px;

    .van-tab__text {
      font-size: 27px;
      color: #777777;
    }
  }
  .van-tabs__line {
    width: 31px !important;
    height: 6px;
    background: rgba(50, 150, 250, 1);
    border-radius: 3px;
    bottom: 8px;
  }
  .van-tabs__nav {
    padding: 0;
  }
}
.hamburger-btn {
  position: fixed;
  right: 0;
  width: 66px;
  height: 82px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #fff;
  opacity: 0.902;
  i.toutiao {
    font-size: 33px;
  }
  &:before {
    content: "";
    position: absolute;
    left: 0;
    width: 1px;
    height: 100%;
    background-image: url(~@/assets/gradient-gray-line.png);
    background-size: contain;
  }
}
// 如果flex忘记了,学习
// (1)教程
// 语法篇:https://www.ruanyifeng.com/blog/2015/07/flex-grammar.html
// 用例篇:https://www.ruanyifeng.com/blog/2015/07/flex-examples.html
// (2)单个属性学习-w3cshool
// https://www.w3school.com.cn/cssref/pr_flex-shrink.asp
//
.placeholder {
  flex-shrink: 0;
  width: 66px;
  height: 82px;
}
</style>
